package menu.lt.pers.menu;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.AbsListView.OnScrollListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import menu.lt.pers.menu.Bean.RecipeClassResultBean;
import menu.lt.pers.menu.Bean.ResultByClassIdBean;
import menu.lt.pers.menu.Bean.UserBean;
import menu.lt.pers.menu.adapter.RecipeListViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class ClassActivity extends FragmentActivity {
    private ListView listViewClass;
    private ListView listViewRecipe;
    private List<RecipeClassResultBean.ResultBean.ListBean> classlist;//分类数据
    private int currentClass = 0;
    private MyListview myListview;
    private RecipeListViewAdapter recipeAdapter;
    private boolean login_state = false;
    private UserBean user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class);
        initView();
        setListener();
        initData();
        upgrateView();
    }



    private void setListener() {
        listViewClass.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (currentClass != position){
                    currentClass = position;
                    myListview.notifyDataSetChanged();
                    recipeAdapter.resetAdapter(classlist.get(currentClass).getClassid());
                }

            }
        });
        listViewRecipe.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ClassActivity.this,RecipeInfo.class);
                Gson gson = new Gson();
                intent.putExtra("recipe",gson.toJson(recipeAdapter.getItem(position)));
                if (login_state){
                    intent.putExtra("user",gson.toJson(user));
                }
                startActivity(intent);
            }
        });
        listViewRecipe.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view,
                                 int firstVisibleItem,
                                 int visibleItemCount,
                                 int totalItemCount) {
                if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
                    View lastVisibleItemView = listViewRecipe.getChildAt(listViewRecipe.getChildCount() - 1);
                    if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == listViewRecipe.getHeight()) {
                        recipeAdapter.addList();
                    }
                }
            }
        });
    }

    private void initView() {
        listViewClass = findViewById(R.id.listView_class);
        listViewRecipe = findViewById(R.id.listView_recipe);
    }
    private void initData() {
        String str = getIntent().getStringExtra("list");
        currentClass = getIntent().getIntExtra("index",0);
        Gson gson = new Gson();
        classlist = gson.fromJson(str,new TypeToken<ArrayList<RecipeClassResultBean.ResultBean.ListBean>>() {}.getType());

        String userStr = getIntent().getStringExtra("user");
        if (userStr!=null){
            login_state = true;
            user = gson.fromJson(userStr,UserBean.class);
        }
    }
    private void upgrateView() {
        myListview = new MyListview(ClassActivity.this,classlist);
        listViewClass.setAdapter(myListview);
        listViewClass.post(new Runnable() {
            @Override
            public void run() {
                listViewClass.smoothScrollToPosition(currentClass);
            }
        });
        recipeAdapter = new RecipeListViewAdapter(ClassActivity.this,
                classlist.get(currentClass).getClassid());
        listViewRecipe.setAdapter(recipeAdapter);
    }
//左边的classAdapter
    public class MyListview extends BaseAdapter {
        private LayoutInflater inflater;
        private List<RecipeClassResultBean.ResultBean.ListBean> list;

        public MyListview(Context context, List<RecipeClassResultBean.ResultBean.ListBean> list) {
            this.inflater = LayoutInflater.from(context);
            this.list = list;
        }
        @Override
        public int getCount() {
            return this.list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.listview_tag, null);
                holder = new ViewHolder();
                holder.tv_line = convertView.findViewById(R.id.tv_line);
                holder.tv_tagname = convertView.findViewById(R.id.tv_tagname);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv_tagname.setText(list.get(position).getName());

            if (currentClass == position){
//                Log.i("ClassActivity.MyListview.getView","currentClass:"+position);
                holder.tv_tagname.setTextColor(getResources().getColor(R.color.theme));
                holder.tv_line.setBackgroundColor(getResources().getColor(R.color.theme));
            } else {
                holder.tv_tagname.setTextColor(getResources().getColor(R.color.black));
                holder.tv_line.setBackgroundColor(getResources().getColor(R.color.crystal));
            }
            return convertView;
        }
        public class ViewHolder {
            public TextView tv_line;
            public TextView tv_tagname;
        }
    }
}
